﻿#region Copyright
// Partial Copyright (c) LogoUI Software Solutions LTD
// Author: Vladislav Spivak
// This source file is the part of LogoFX Framework http://logofx.codeplex.com
// See accompanying licences and credits.
#endregion


using System;
using System.Windows;
#if !WinRT
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Interactivity;
using LogoFX.Core;
using EventTrigger = System.Windows.Interactivity.EventTrigger;
#else
using LogoFX.Core;
using Windows.UI.Interactivity;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml.Media;
using EventTrigger = Windows.UI.Interactivity.EventTrigger;
#endif


namespace LogoFX.Infra.Interactivity
{
    /// <summary>
    /// Makes object invisible when some value became less than provided settings
    /// </summary>
    [Obsolete("Use Microsoft.Expression.Interactivity.Core.DataTrigger if possible")]
    public class LessToVisibilityBeaviour:Behavior<FrameworkElement>
    {

        #region Source dependency property

        public double Source
        {
            get { return (double) GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source", typeof (double), typeof (LessToVisibilityBeaviour), new PropertyMetadata(default(double), OnSourceChanged));

        private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            LessToVisibilityBeaviour ltvb = (LessToVisibilityBeaviour) d;
            if (ltvb.AssociatedObject == null)
                return;
            ltvb.ApplyVisibility();
        }

        #endregion
        #region Value dependency property
        public double Value
        {
            get { return (double) GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof (double), typeof (LessToVisibilityBeaviour), new PropertyMetadata(0.0, OnValueChanged));

        private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            LessToVisibilityBeaviour ltvb = (LessToVisibilityBeaviour)d;
            if (ltvb.AssociatedObject == null)
                return;
            ltvb.ApplyVisibility();
        }

        #endregion


        public bool IsRevert
        {
            get { return (bool)GetValue(IsRevertProperty); }
            set { SetValue(IsRevertProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsRevert.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsRevertProperty =
            DependencyProperty.Register("IsRevert", typeof(bool), typeof(LessToVisibilityBeaviour), new PropertyMetadata(false));

        
        protected override void OnAttached()
        {
            ApplyVisibility();
            base.OnAttached();
        }

        private void ApplyVisibility()
        {
            if (IsRevert == false)
            {
                if (Value > Source)
                {
                    AssociatedObject.Visibility = Visibility.Collapsed;
                }
                else
                {
                    AssociatedObject.Visibility = Visibility.Visible;
                }
            }
            else
            {
                if (Value > Source)
                {
                    AssociatedObject.Visibility = Visibility.Visible;
                }
                else
                {
                    AssociatedObject.Visibility = Visibility.Collapsed;
                }
            }

        }
    }
}
